
package com.rt.schedulenew.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rt.schedulebase.dto.DictPrintPatternDto;
import com.rt.schedulebase.dto.DictSchedulePrintConfigDto;
import com.rt.schedulebase.entity.DictPrintPattern;
import com.rt.schedulebase.entity.DictSchedulePrintConfig;
import com.rt.schedulebase.mapper.DictPrintPatternMapper;
import com.rt.schedulebase.mapper.DictSchedulePrintConfigMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.base.CommonUtil;

import java.util.ArrayList;

import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.api.IDictSerialService;
import org.springframework.beans.factory.annotation.Autowired;
import com.rt.schedulenew.api.IDictPrintPatternService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class DictPrintPatternServiceImpl extends ServiceImpl<DictPrintPatternMapper, DictPrintPattern> implements IDictPrintPatternService {
    @Autowired
    DictSchedulePrintConfigMapper dictSchedulePrintConfigMapper;
    @Autowired
    IDictSerialService dictSerialService;

    @Override
    public JsonResult getPrintPattern(DictPrintPatternDto dto) {
        List<DictPrintPatternDto> list = baseMapper.getPatternList(dto);
        List<DictPrintPatternDto> newList = new ArrayList<DictPrintPatternDto>();
        for (DictPrintPatternDto printPatternDto : list) {
            printPatternDto.setTerminalTypes(CommonUtil.split2List(printPatternDto.getTerminalType()));
            printPatternDto.setQueueNames(CommonUtil.split2List(printPatternDto.getQueueName()));
            newList.add(printPatternDto);
        }
        return JsonResultUtil.success("成功", newList);
    }

    @Override
    public JsonResult insert(DictPrintPatternDto dto) {
        String patternId = dictSerialService.getSerialId("PATTERN_ID");
        String configId = dictSerialService.getSerialId("CONFIG_ID");
        dto.setPatternId(patternId);
        int i = baseMapper.insert(dto);
        int j = 0;
        if (i > 0) {
            DictSchedulePrintConfigDto config = new DictSchedulePrintConfigDto();
            config.setConfigId(configId);
            config.setPatternId(patternId);
            config.setExamClass(dto.getExamClass());
            config.setPatientSource(dto.getPatientSource());
            config.setQueueName(dto.getQueueName());
            config.setTerminalType(dto.getTerminalType());
            j = dictSchedulePrintConfigMapper.insert(config);
        }
        if (j > 0) {
            return JsonResultUtil.success("添加成功");
        }
        return JsonResultUtil.failure("添加失败");
    }

    @Override
    public JsonResult updatePattern(DictPrintPatternDto dto) {
        if (StringUtils.isBlank(dto.getPatternId())) {
            return JsonResultUtil.failure("模板ID不能外空");
        }
        UpdateWrapper<DictPrintPattern> wrapper = new UpdateWrapper<>();
        wrapper.eq("PATTERN_ID", dto.getPatternId());
        int count = baseMapper.update(dto, wrapper);
        if (count > 0) {
            DictSchedulePrintConfig config = new DictSchedulePrintConfig();
            config.setPatternId(dto.getPatternId());
            config.setExamClass(dto.getExamClass());
            config.setPatientSource(dto.getPatientSource());
            config.setQueueName(dto.getQueueName());
            config.setTerminalType(dto.getTerminalType());


            QueryWrapper<DictSchedulePrintConfig> queryWrapper = new QueryWrapper<>();
            List<DictSchedulePrintConfig> configs = dictSchedulePrintConfigMapper.selectList(queryWrapper);
            if (CollectionUtils.isNotEmpty(configs)) {
                UpdateWrapper<DictSchedulePrintConfig> configUpdateWrapper = new UpdateWrapper<>();
                configUpdateWrapper.set(StringUtils.isBlank(config.getQueueName()), "QUEUE_NAME", "");
                configUpdateWrapper.set(StringUtils.isBlank(config.getPatientSource()), "PATIENT_SOURCE", "");
                configUpdateWrapper.eq("PATTERN_ID", dto.getPatternId());

                count = dictSchedulePrintConfigMapper.update(config, configUpdateWrapper);
            } else {
                count = dictSchedulePrintConfigMapper.insert(config);
            }
        }
        if (count > 0) {
            return JsonResultUtil.success("修改成功");
        }
        return JsonResultUtil.failure("修改失败");
    }

    @Override
    public JsonResult delPattern(DictPrintPatternDto dto) {
        if (StringUtils.isBlank(dto.getPatternId())) {
            return JsonResultUtil.failure("模板编号不能为空");
        }
        QueryWrapper<DictPrintPattern> pattern = new QueryWrapper<>();
        pattern.eq("PATTERN_ID", dto.getPatternId());
        int i = baseMapper.delete(pattern);
        int count = 0;
        if (i > 0) {
            QueryWrapper<DictSchedulePrintConfig> config = new QueryWrapper<>();
            config.eq("PATTERN_ID", dto.getPatternId());
            count = dictSchedulePrintConfigMapper.delete(config);
        }
        if (count > 0) {
            return JsonResultUtil.success("删除成功");
        }
        return JsonResultUtil.failure("删除失败");
    }


    @Override
    public List<DictPrintPatternDto> getPatternList(DictPrintPatternDto dto) {
        List<DictPrintPatternDto> list = baseMapper.getPatternList(dto);
        for (DictPrintPatternDto printPatternDto : list) {
            printPatternDto.setTerminalTypes(CommonUtil.split2List(printPatternDto.getTerminalType()));
            printPatternDto.setQueueNames(CommonUtil.split2List(printPatternDto.getQueueName()));
        }
        return list;
    }

}
